package com.nf.xiaomi.web.controller;

import com.nf.mvc.ViewResult;
import com.nf.mvc.argument.RequestParam;
import com.nf.mvc.mapping.RequestMapping;
import com.nf.mvc.support.ResponseVO;
import com.nf.xiaomi.entity.ProductEntity;
import com.nf.xiaomi.service.ProductInfoService;
import com.nf.xiaomi.service.ProductService;
import com.nf.xiaomi.service.impl.ProductInfoServiceImpl;
import com.nf.xiaomi.service.impl.ProductServiceImpl;
import com.nf.xiaomi.vo.ProductVo;

import java.util.List;

import static com.nf.mvc.handler.HandlerHelper.json;

@RequestMapping("/product")
public class ProductController {
    ProductInfoService productInfoService =new ProductInfoServiceImpl();
    ProductService productService = new ProductServiceImpl();
    @RequestMapping("/typeAll")
    public ViewResult getTypeAll(String typeName){
        List<ProductEntity> typeAll = productService.typeAll(typeName);
        return json(new ResponseVO(200, "ok", typeAll));
    }

    @RequestMapping("/getAll")
    public ViewResult getAll(){
        List<ProductEntity> all = productService.getAll();
        return json(new ResponseVO(200, "ok", all));
    }

    @RequestMapping("/getAllName")
    public ViewResult getSearchAll(String name) {
        List<ProductEntity> products = productService.getAll(name);
        return json(new ResponseVO(200, "ok", products));
    }
    @RequestMapping("/info")
    public ViewResult getProductModel(int pid, @RequestParam(defaultValue = "%%") String memory){
        ProductVo productInfo = productInfoService.getAttribute(pid, memory);
        return json(ResponseVO.success(productInfo));
    }
    @RequestMapping("/detail")
    //获取商品详细的id和价格
    public ViewResult getGoods(int pid, String memory, String color){
        ProductEntity productInfo = productInfoService.getProductInfo(pid, memory, color);
        return json(ResponseVO.success(productInfo));
    }
}
